package com.iris.live.services.schedule;

import com.iris.live.services.data.repositories.Lsh经销商表Repository;
import com.iris.live.services.data.repositories.WeekReprotRepository;
import com.iris.live.services.services.TimedTaskService;
import com.iris.live.services.services.impl.ApprovalServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Component
public class ReportSchecule {

    @Autowired
    private static Logger logger = LoggerFactory.getLogger(ApprovalServiceImpl.class);

    @Autowired
    private TimedTaskService timedTaskService;

    @Autowired
    private Lsh经销商表Repository lsh经销商表Repository;

    @Autowired
    private com.iris.live.services.data.repositories.TWeeklySm2RosRptRepository tWeeklySm2RosRptRepository;

    @Autowired
    private WeekReprotRepository weekReprotRepository;

    @Transactional
   // @Scheduled(cron = "0 50 00 * * ?")
    public void sendMsgListTask() {
        //每日凌晨00:30分执行
        try {
            logger.info("*******************************开票、付款、收款定时提醒任务START*******************************");
            timedTaskService.getSendMsgListTask();
            logger.info("*******************************开票、付款、收款定时提醒任务END*******************************");
        } catch (Exception e) {
            logger.error("开票、付款、收款定时任务执行失败！");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

/*    @Transactional
    @Scheduled(cron = "0 50 00 * * ?")
    public void setWeelySmRosReport() {
        //每日凌晨执行
        try {
            logger.info("*******************************集团周报表定时任务START*******************************");
        timedTaskService.setWeelySmRosReport();
            logger.info("*******************************集团周报表定时任务STARTEND*******************************");
        } catch (Exception e) {
            logger.error("*集团周报表定时任务执行失败！");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }*/

   // @Scheduled(cron = "0 10 00 * * ?")
    public void setActualFinanceServeFree() {
        logger.info("*******************************金融服务费实际值任务START*******************************");
        timedTaskService.setActualFinanceServeFree();
        logger.info("*******************************金融服务费实际值任务END*******************************");
    }
   // @Scheduled(cron = "0 40 00 * * ?")
    public void setFlowupReport() {
        logger.info("*******************************当日线索跟进表手动任务START*******************************");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new java.util.Date());
        try {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            String date=sdf1.format(calendar.getTime());
            timedTaskService.setFlowupReport(date);
        }
        catch (Exception e){
            logger.info("当日线索跟进表手动任务失败");
        }
        logger.info("*******************************当日线索跟进表手动任务END*******************************");
    }

    /**
     * 凌晨两点更新前一天
     * 离店时间为空的进店记录
     *
     */
  //  @Scheduled(cron = "0 0 2 * * ?")
    public void updateLeaveTime(){
        logger.info("*******************************更新离店时间为空的进店记录任务START*******************************");
        try{
            lsh经销商表Repository.updateLeaveTime();
            logger.info("*******************************更新离店时间为空的进店记录任务end*******************************");
        }catch(Exception e){
            logger.error("**********************更新离店时间为空的进店记录任务异常**********************");
        }
    }
}

